package com.weaver.cli;

import com.weaver.model.Model;

import java.util.List;
import java.util.Scanner;

/**
 * CLI class - Command Line Interface version
 */
public class CLI {
    private Model model;
    private Scanner scanner;
    
    /**
     * Constructor
     * @param model game model
     */
    public CLI(Model model) {
        this.model = model;
        this.scanner = new Scanner(System.in);
    }
    
    /**
     * Run the game
     */
    public void run() {
        System.out.println("╔════════════════════════════════════╗");
        System.out.println("║   Welcome to Lexical Transmogrifier  ║");
        System.out.println("╚════════════════════════════════════╝");
        System.out.println("Embark on a journey of letter alchemy -");
        System.out.println("Morph the origin term into its final form");
        System.out.println("through sequential character permutations");
        System.out.println("Each transitional state must be a valid lexeme");
        System.out.println();

        System.out.println("┌──────────────────────────────┐");
        System.out.printf("│ %-12s ➤ %-15s │\n", "Origin Term", model.getStartWord());
        System.out.printf("│ %-12s ➤ %-15s │\n", "Terminus", model.getTargetWord());
        System.out.println("└──────────────────────────────┘");
        System.out.println();


        // If show path option is enabled, display the path
        if (model.isShowPath()) {
            List<String> path = model.findPath();
            if (!path.isEmpty()) {
                System.out.println("Path: ");
                for (String word : path) {
                    System.out.print(word + " → ");
                }
                System.out.println("Complete");
                System.out.println();
            }
        }
        
        // Game loop
        String currentWord = model.getStartWord();
        while (!model.isGameWon()) {
            System.out.println("Current word: " + currentWord);
            System.out.print("Please enter the next word: ");
            String input = scanner.nextLine().trim().toUpperCase();
            
            // Check if input is empty
            if (input.isEmpty()) {
                System.out.println("Please enter a word");
                continue;
            }
            
            // Try to submit the word
            boolean success = model.tryWord(input);
            if (success) {
                currentWord = input;
                
                // Display feedback
                List<String[]> feedbacks = model.getFeedbacks();
                String[] feedback = feedbacks.get(feedbacks.size() - 1);
                System.out.print("Feedback: ");
                for (int i = 0; i < feedback.length; i++) {
                    if (feedback[i].equals("correct")) {
                        System.out.print(currentWord.charAt(i) + "(correct) ");
                    } else {
                        System.out.print(currentWord.charAt(i) + "(wrong) ");
                    }
                }
                System.out.println();
            }
            
            System.out.println();
        }
        
        // Game won
        System.out.println("Congratulations! You successfully transformed " + model.getStartWord() + " into " + model.getTargetWord());
        System.out.println("You completed the game in " + model.getAttemptsCount() + " steps");
    }
}